/**
 * 首页状态管理
 */
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { Banner, fetchBanners } from '../services/banner';

export const useIndexStore = defineStore('index', () => {
  // 状态
  const banners = ref<Banner[]>([]);
  const loading = ref(false);
  const error = ref<Error | null>(null);

  // 方法
  const getBanners = async () => {
    loading.value = true;
    error.value = null;

    try {
      banners.value = await fetchBanners();
    } catch (err) {
      error.value = err as Error;
      console.error('获取轮播图失败:', err);
    } finally {
      loading.value = false;
    }
  };

  // 重置状态
  const resetState = () => {
    banners.value = [];
    loading.value = false;
    error.value = null;
  };

  return {
    // 状态
    banners,
    loading,
    error,

    // 方法
    getBanners,
    resetState
  };
});
